<template>
  <div>
    <div>
      <!-------------------------------表格 ----------------------down ------------------->
      <div v-show="!editVisible" class="container">
        <!-------------------------------按钮 ----------------------down------------------->
        <el-card>
            <el-button class="hand-btn" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
            <el-input v-model="query.name" class="handle-input" placeholder="产品名称" />
            <el-button class="hand-btn" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-card>
        <!-------------------------------按钮 ----------------------up ------------------->
        <el-table
          ref="multipleTable"
          :data="page.content"
          border
          class="table"
          header-cell-class-name="table-header"
        >
          <el-table-column prop="id" label="产品ID" />
          <el-table-column prop="code" label="产品编码" />
          <el-table-column prop="name" label="产品名称" />
          <el-table-column prop="describe" label="产品描述" />
          <el-table-column label="操作" width="180" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button>
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            layout="total, prev, pager, next"
            :current-page="query.pageIndex"
            :page-size="query.pageSize"
            :total="page.totalElements"
            @current-change="handlePageChange"
          />
        </div>
      </div>
      <!-------------------------------表格 ----------------------up ------------------->
      <!----------------------------------form 表单------------------down----------- -->
      <div v-show="editVisible">
        <!-------------------------------按钮 ----------------------down------------------->
        <div v-show="editVisible" class="handle-box">
          <el-button class="hand-btn" type="primary" icon="el-icon-back" @click="editVisible = !editVisible">返回</el-button>
          <el-button class="hand-btn" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
          <el-button class="hand-btn" type="primary" @click="saveEdit">保存&返回</el-button>
        </div>
        <!-------------------------------按钮 ----------------------up ------------------->
        <el-form ref="form" :model="form"  label-width="100px">
          <el-form-item label="产品编码" prop="code">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="产品名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="产品描述" prop="describe">
            <el-input v-model="form.describe" />
          </el-form-item>
        </el-form>
      </div>
      <!-------------------------------form表单----------------------up ------------------->
    </div>
  </div>
</template>
<script>import { save } from '@/api/baseProduct'
import { query } from '@/api/baseProduct'
import { delbyid } from '@/api/baseProduct'
export default {
  name: 'BaseProduct',
  data() {
    return {
      editVisible: false,
      query: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      page: {},
      form: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      query(this.query).then(res => {
        console.log(res)
        this.page = res.data.page
      })
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1)
      this.getData()
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        delbyid(row.id).then(res => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
        .catch(() => {
        })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    delAllSelection() {
      const length = this.multipleSelection.length
      let str = ''
      this.delList = this.delList.concat(this.multipleSelection)
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' '
      }
      this.$message.error(`删除了${str}`)
      this.multipleSelection = []
    },
    // 编辑操作
    handleEdit(index, row) {
      this.form = row
      this.editVisible = true
    },
    // 保存编辑
    saveEdit() {
      save(this.form).then(res => {
        this.$message.success(res.message)
        this.editVisible = false
        this.getData()
      })
    },
    add() {
      this.form = {}
      this.editVisible = true
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val)
      this.getData()
    }
  }
}
</script>

<style scoped>
  .handle-box {
    margin: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    margin: 10px;
    width: 300px;
    display: inline-block;
  }
  .table {
    width: 100%;
    font-size: 14px;
  }
  .red {
    color: #ff0000;
  }
  .hand-btn {
    margin: 5px;
  }
</style>
